﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session.RemoveAll();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string w1 = (string)Session["valid"];
        if (w1 == TextBox3.Text.ToUpper())
        {
            int? i;
            DataClassesDataContext db1 = new DataClassesDataContext();
            string phone=TextBox1.Text.ToString();
            string psw = TextBox2.Text.ToString();
            i = db1.proc_LoginCheck(phone, psw);
            Response.Write("<script>alert(i)</script>");
            if (i == 1)
            {
                var resultA = (from p in db1.proc_GetAdmInfobyPwd(phone)
                              select p).First();
                Session["username"] = Convert.ToString(resultA.userName);
                Session["name"] = resultA.tname;
                Session["phone"] = phone;           //管理员
                Session["psw"] = psw;
                Session["type"] = "1";
                Response.Redirect("main.aspx");
            }
            else if (i == 0)
            {
                var result = (from p in db1.proc_GetStuInfobyPwd(phone)
                               select p).First();
                Session["name"] = result.sname;
                Session["wbh"] = result.wbh;
                Session["phone"] = phone;            //学生
                Session["psw"] = psw;
                Session["type"] = "0";
                Response.Redirect("main.aspx");
            }
            else
            {
                Response.Write("<script>alert('用户名或密码错误！')</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('验证码错误！')</script>");
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Student/register.aspx");
    }
}